package com.itheima.Test;

import com.itheima.util.FileUtils;
import com.itheima.util.RedisConstant;
import com.itheima.util.RedisUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

@JobHandler(value = "job.test")
@Component
public class MyTestJob extends IJobHandler {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
//        System.out.println("my test ...");
        //用set存储数据
        //取出所有元素：（file:allset
//        Set<Object> allset = redisTemplate.opsForSet().members("file:allset");
        Set<Object> allset = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        if (allset!=null){
            for (Object path: allset) {
                String filekey="file:"+path;
                if (redisTemplate.hasKey(filekey)==false){
                    //如果发现没有改数据，证明过期，删除
                    FileUtils.deletefile((String)path);
                    //从临时节点移出去
//                    redisTemplate.opsForSet().remove("file:allset",path);
                    RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,path);
                }
            }
        }
        return SUCCESS;
    }

}
